/**
 *岗位编辑新增js
 */
layui.use(['jquery','febs', 'form', 'commonJS','validate'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        // commonJS = layui.commonJS,
        $view = $('#post-edit'),
        validate = layui.validate;

    let tenantId = POPUP_DATA.tenantId
    let tenantName = POPUP_DATA.tenantName;
    form.verify(validate);
    form.render();
    initFormValue();

    function initFormValue() {
        if (post) {
            form.val("post-edit-form", {
                "postId": post.postId,
                "tenantId": post.tenantId,
                "postCode": post.postCode,
                "postName": post.postName,
                "sort": post.sort,
                "remark": post.remark
            });
            //根基后台status值渲染开关
            if(post.status === '1'){
                $view.find('input[name=status]').attr("checked","true");
                form.render('checkbox');
            }
            $view.find("#tenantName").html(post.tenantName)
        }else {
            form.val("post-edit-form", {
                "tenantId": tenantId,
                status:true
            });
            $view.find("#tenantName").html(tenantName)
        }
    }


    form.on('submit(post-edit-form-submit)', function (data) {
        if(data.field.status == "on"){
            data.field.status = '1';
        }else{
            data.field.status = '0';
        }
        addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {
        //修改删除岗位
        if (data.postId && data.postId.length > 0) {
            febs.modal.confirm('修改岗位', '确定修改岗位？', function () {
                febs.post(ctx + 'post/update', data, function () {
                    layer.closeAll();
                    febs.alert.success('修改成功');
                    $('#febs-post').find('#query').trigger('click');
                });
            });
        } else {
            febs.modal.confirm('新增岗位', '确定新增岗位？', function () {
                febs.post(ctx + 'post/add', data, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                    $('#febs-post').find('#query').trigger('click');
                });
            });
        }
    }

});